package com.shenmazong.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.shenmazong.pojo.TbGoods;
import com.shenmazong.service.TbGoodsService;
import com.shenmazong.mapper.TbGoodsMapper;
import com.shenmazong.utils.ResultResponse;
import com.shenmazong.vo.GoodsInfoVo;
import com.shenmazong.vo.LoginVo;
import com.shenmazong.vo.PageVo;
import lombok.SneakyThrows;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.UUID;

/**
 *
 */
@Service
public class TbGoodsServiceImpl extends ServiceImpl<TbGoodsMapper, TbGoods>
    implements TbGoodsService{

    @Autowired
    TbGoodsMapper tbGoodsMapper;

    @Autowired
    RabbitTemplate rabbitTemplate;

    @Override
    public ResultResponse getGoodsByPage(PageVo pageVo) {

        Page<TbGoods> tbGoodsPage = new Page<>(pageVo.getPageNum(), pageVo.getPageSize());
        LambdaQueryWrapper<TbGoods> queryWrapper = new QueryWrapper<TbGoods>().lambda()
                .eq(TbGoods::getSellerId, pageVo.getId())
                .orderByDesc(TbGoods::getGoodsId);
        Page<TbGoods> goodsPage = tbGoodsMapper.selectPage(tbGoodsPage, queryWrapper);

        Page<GoodsInfoVo> goodsInfoVoPage = new Page<GoodsInfoVo>();
        ArrayList<GoodsInfoVo> goodsInfoVos = new ArrayList<>();
        goodsPage.getRecords().forEach(goods -> {
            GoodsInfoVo goodsInfoVo = new GoodsInfoVo();
            BeanUtils.copyProperties(goods, goodsInfoVo);
            goodsInfoVos.add(goodsInfoVo);
        });

        BeanUtils.copyProperties(goodsPage, goodsInfoVoPage);
        goodsInfoVoPage.setRecords(goodsInfoVos);

        return ResultResponse.SUCCESS(goodsInfoVoPage);
    }

    @SneakyThrows
    @Override
    public ResultResponse testSet() {

        String uuid = UUID.randomUUID().toString();

        LoginVo loginVo = new LoginVo();
        loginVo.setUserName("jidongcheng");
        loginVo.setPassWord("123456");



        rabbitTemplate.convertAndSend("shopExchange", "shopper.a", loginVo);
        rabbitTemplate.convertAndSend("shopExchange", "html.b", "woyaoshenghtml");
        return ResultResponse.SUCCESS();
    }
}




